{template 'web/common/header'}
<style>
    .layui-input-required{color:#ff0000;font-weight:800;}
</style>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">
                    <span>{$title}</span>
                    <button class="layui-btn layui-btn-sm" data-type="storageAdd" id="storageAdd">增加</button>
                </div>
                <div class="layui-card-body">
                    <table class="layui-hide" id="storage-table" lay-filter="storage"></table>
                    <script type="text/html" id="storage-table-operate-bar">
                    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">编辑</a>
                    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                  </script>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="../addons/xn_erp/images/layuiadmin/layui/layui.js"></script>
<script>
    layui.use(['table', 'element', 'form'], function () {
        var table = layui.table, $ = layui.$, element = layui.element, form = layui.form;
        element.render();
        // 渲染表格
        storage = table.render({
            elem: '#storage-table'
            , url: '{$url}&api=list'
            , title: '{$title}'
            , cols: [[

                { field: 'sname', title: '仓库名称', edit: 'text' }
                , { field: 'snumber', title: '仓库编号', edit: 'text' }
                , { field: 'address', title: '仓库地址', edit: 'text' }
                , { field: 'phone', title: '仓库电话', edit: 'text' }
                , { width: 178, align: 'center', title: "操作", fixed: 'right', toolbar: '#storage-table-operate-bar' }
            ]]
            , page: true
            , limit: 20
            , method: 'post'
            , id: 'storageReload'
        });
        // 删除
        table.on('tool(storage)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    $.post("{$url}&api=del", { sid: data.id }, function (result) { layer.msg(result); });
                    obj.del();
                    layer.close(index);
                });
            }
            // 编辑
            if (obj.event === 'edit') {
                storageform = layer.open({
                    type: 1,
                    title: '编辑仓库',
                    content: $('#storageform')
                });

                for (var i in data) {
                    $("input[name=" + i + "]").val(data[i]);
                }
                $("input[name=sid]").val(data.id);
                $("input[name=api]").val("edit");
            }
        });
        // 编辑
        table.on('edit(storage)', function (obj) {
            var data = obj.data;
            data['sid'] = data.id;
            $.post("{$url}&api=edit", data, function (result) { layer.msg(result); });
        });
        // 增加
        $('#storageAdd').on('click', function () {
            $("input[type=reset]").trigger("click");
            $("input[name=api]").val("add");
            storageform = layer.open({
                type: 1,
                title: '添加仓库',
                content: $('#storageform')
            });

        });

        form.on('submit(storageform)', function (data) {
            $.post("{$url}&api=" + data.field.api, data.field, function (result) { layer.msg(result); });
            layer.close(storageform);
            storage.reload();
            return false;
        });

    });
</script>

</body>
<div class="layui-fluid" style="display:none;padding-right: 50px;" id="storageform">
    <form class="layui-form" action="">
        <input type="hidden" name='api'>
        <input type="hidden" name='sid'>
        <div class="layui-form-item">
            <label class="layui-form-label"><span class="layui-input-required">*</span>仓库名称</label>
            <div class="layui-input-block">
                <input type="text" name="sname" required lay-verify="required" placeholder="请输入仓库名称" autocomplete="off"
                    class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">仓库编号</label>
            <div class="layui-input-block">
                <input type="text" name="snumber" required placeholder="请输入仓库编号" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">仓库地址</label>
            <div class="layui-input-block">
                <input type="text" name="address" required placeholder="请输入仓库地址" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">仓库电话</label>
            <div class="layui-input-block">
                <input type="text" name="phone" required placeholder="请输入仓库电话" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="storageform">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>

</html>